﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 作者: Foldcc
/// QQ: 1813547935
/// </summary>
public class UIController : MonoBehaviour {
	public GameObject[] UIview;

	public GameObject[] UIPlayers;

    public AudioClip startBackgroundAudio;

    public AudioClip backgroundAudio;

	public AudioClip loadAudio;

	public AudioClip lossAudio;

	public static int gameLevel = 0;

	public static int enemyCount = 0;

	public static int playerHp = 0;

	public static int gameTime = 0;

	public Text levelText;
	public Text gameTimeText;
    public Text[] playerScoreTxts;
	private int[] scoreValues;
	public Text gameoverTxt;

	private void Start() {
		foreach(var player in UIPlayers)
			player.SetActive(false);
	}
    
    


    public void StartViewWithIndex(int index){
		for(int i = 0 ; i< UIview.Length ; i++){
			UIview [i].SetActive (false);
		}
		UIview [index].SetActive (true);
        if (index == 0)
        {
            GetComponent<AudioSource>().loop = true;
            GetComponent<AudioSource>().clip = startBackgroundAudio;

        }else if (index == 1) {
            GetComponent<AudioSource>().loop = true;
            GetComponent<AudioSource> ().clip = backgroundAudio;
            
        } else if (index == 2) {
			GetComponent<AudioSource> ().clip = loadAudio;
		} else if (index == 3) {
            GetComponent<AudioSource>().loop = false;
            GetComponent<AudioSource> ().clip = lossAudio;
			// 设置谁获胜
			int playerWinIndex = playerWinNum();
			if(playerWinIndex == 0)
           		gameoverTxt.text = "爱丽丝获胜了";
			else if(playerWinIndex == 1)
           		gameoverTxt.text = "小红帽获胜了";
			else if(playerWinIndex == 2)
           		gameoverTxt.text = "灰姑娘获胜了";
			else if(playerWinIndex == 3)
           		gameoverTxt.text = "白雪公主获胜了";
			
			UIPlayers[playerWinIndex].SetActive(true);
		}
		GetComponent<AudioSource> ().Stop ();
		GetComponent<AudioSource> ().Play ();

	}
	public void initScoreValue()
	{
		scoreValues = new int[4]{0, 0, 0, 0};
		for(int i = 1; i <= 4; i++)
		{
			playerScoreTxts[i - 1].text = "0";
		}
	}

	public void SetScore(int _playerIdx, int _score){
		scoreValues[_playerIdx] = _score;
		playerScoreTxts[_playerIdx].text = _score.ToString();
	}

	public bool isGameFinish()
	{
		int curMax = scoreValues[0];
		bool isFinish = true;
		for(int i = 1; i < scoreValues.Length; i++)
		{
			if(scoreValues[i] > curMax)
			{
				curMax = scoreValues[i];
				isFinish = true;
			}
			else if (scoreValues[i] == curMax)
			{
				isFinish = false;
			}
		}
		return isFinish;
	}

	public int playerWinNum()
	{
		int curMax = scoreValues[0];
		int index = 0;
		for(int i = 1; i < scoreValues.Length; i++)
		{
			if(scoreValues[i] > curMax)
			{
				curMax = scoreValues[i];
				index = i;
			}
		}
		return index;
	}

	void OnGUI(){
		//levelText.text = "Level: " + gameLevel;
		gameTimeText.text = "Time: " + gameTime;
		if (gameTime < 15)
			gameTimeText.color = Color.red;
		else
			gameTimeText.color = Color.white;
        

        //if (Input.anyKeyDown)
        //{
        //    Event e = Event.current;
        //    if (e.isKey)
        //    {
        //        Debug.Log("Current Key is : " + e.keyCode.ToString());
        //    }
        //}
    }

}
